//
//  UIView+ZFBadge.m
//  ZFBadgeView
//
//  Created by fyzq on 2021/9/6.
//

#import "UIView+ZFBadge.h"
#import <objc/runtime.h>
const void *keyBadgeView = @"keyBadgeView";
const void *keyBadgeTipView = @"keyBadgeTipView";
const void *keyItems = @"keyItems";
const void *keyPosition = @"keyPosition";
const void *keySize = @"keySize";
@interface UIView ()
@property(nonatomic,copy)NSMutableArray *items;

@end
@implementation UIView (ZFBadge)


///设置显示位置左上角开始（5，5）=>  即 距离右边 5 距离顶部5
-(void)zf_setBadgeViewPosition:(CGPoint)position{
    if (![NSStringFromCGPoint(self.badgeView.position) isEqualToString:NSStringFromCGPoint(position)]) {
        self.badgeView.position = position;
        [self layoutBadge];
    }
}
///设置标签文字
-(void)zf_setBadgeViewText:(NSString *)text{
    self.badgeView.text = text;
}
///设置标签文字颜色
-(void)zf_setBadgeViewTextColor:(UIColor *)textColor{
    self.badgeView.textColor = textColor;
}
///设置标签文字大小
-(void)zf_setBadgeViewFont:(UIFont *)font{
    self.badgeView.font = font;
}
///设置背景颜色
-(void)zf_setBadgeViewBackgroudColor:(UIColor *)color{
    self.badgeView.bgColor = color;
}
///设置背景颜色
-(void)zf_setBadgeViewBackgroundImage:(UIImage *)image{
    self.badgeView.image = image;
}
///设置内边距(0,5,0,5)
-(void)zf_setBadgeViewEdgeInsets:(UIEdgeInsets )edgeInsets{
    self.badgeView.edgeInsets = edgeInsets;
}


//////////////////////////////////////////////////////////////////////////////////////////////////////
///这是一个闪烁动画
///设置一个tip动画 默认白色
-(void)zf_setBadgeAnimateViewColor:(UIColor *)color{
    self.badgeAnimateView.color = color;
}
///设置显示大小
-(void)zf_setBadgeAnimateViewSize:(CGSize)size{
    if (![NSStringFromCGSize(self.badgeAnimateView.size) isEqualToString:NSStringFromCGSize(size)]) {
        self.badgeAnimateView.size = size;
        [self addLayoutConstraint];
    }
}
///设置显示位置左上角开始（5，5）=>  即 距离右边 5 距离顶部5
-(void)zf_setBadgeAnimateViewPosition:(CGPoint)position{
    if (![NSStringFromCGPoint(self.badgeAnimateView.position) isEqualToString:NSStringFromCGPoint(position)]) {
        self.badgeAnimateView.position = position;
        [self addLayoutConstraint];
    }
}
///设置放大比例
-(void)zf_setBadgeAnimateViewScale:(CGFloat)scale{
    self.badgeAnimateView.scale = scale;
}
///隐藏这个tipview(移除)
-(void)zf_hiddenBadgeAnimateView{
    [self.badgeAnimateView removeFromSuperview];
}
///展示
-(void)zf_showBadgeAnimateView{
    [self zf_setBadgeAnimateViewColor:UIColor.redColor];
}



#pragma mark --- 判断字符串是不是整型
- (BOOL)isPureInt:(NSString *)string{
    NSScanner* scan = [NSScanner scannerWithString:string];
    int val;
    return [scan scanInt:&val] && [scan isAtEnd];
}



#pragma mark --- 移除
-(void)removeClass:(Class)class{
    for (UIView *view in self.subviews) {
        if ([view isKindOfClass:class]) {
            [view removeFromSuperview];
        }
    }
}
#pragma mark --- 布局
-(void)layoutBadge{
    [self removeConstraints:self.items];
    [self.items removeAllObjects];
    CGPoint position = self.badgeView.position;
    
    [ self.items addObjectsFromArray:[NSLayoutConstraint constraintsWithVisualFormat:@"H:[badgeControl]-space-|" options:0 metrics:@{@"space":@(position.x)} views:@{@"badgeControl":self.badgeView}]];
    [ self.items addObjectsFromArray:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-space-[badgeControl]" options:0 metrics:@{@"space":@(position.y)} views:@{@"badgeControl":self.badgeView}]];

    [self addConstraints:self.items];
}
-(void)addLayoutConstraint{
    [self removeConstraints:self.items];
    [self.items removeAllObjects];
    CGSize size = self.badgeAnimateView.size;
    CGPoint position = self.badgeAnimateView.position;
    [ self.items addObjectsFromArray:[NSLayoutConstraint constraintsWithVisualFormat:@"H:[badgeControl(==size)]-space-|" options:0 metrics:@{@"size":@(size.width),@"space":@(position.x)} views:@{@"badgeControl":self.badgeAnimateView}]];
    [ self.items addObjectsFromArray:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-space-[badgeControl(==size)]" options:0 metrics:@{@"size":@(size.height),@"space":@(position.y)} views:@{@"badgeControl":self.badgeAnimateView}]];
    [self addConstraints:self.items];
}





#pragma mark - setter/getter

- (ZFBadgeStretchView *)badgeView{
    ZFBadgeStretchView *badgeView = objc_getAssociatedObject(self, &keyBadgeView);
    if (!badgeView) {
        [self removeClass:ZFBadgeAnimationView.class];
        badgeView = [ZFBadgeStretchView new];
        badgeView.translatesAutoresizingMaskIntoConstraints = NO;
        [self addSubview:badgeView];
        [self bringSubviewToFront:badgeView];
        [self setBadgeView:badgeView];
        [self layoutBadge];
    }
    return badgeView;
}



- (void)setBadgeView:(ZFBadgeStretchView *)badgeView{
    objc_setAssociatedObject(self, &keyBadgeView, badgeView, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

- (ZFBadgeAnimationView *)badgeAnimateView{
    ZFBadgeAnimationView *badgeView = objc_getAssociatedObject(self, &keyBadgeTipView);
    if (!badgeView) {
        [self removeClass:ZFBadgeControl.class];
        badgeView = [ZFBadgeAnimationView new];
        badgeView.scale = 2.5;
        badgeView.translatesAutoresizingMaskIntoConstraints = NO;
        [self bringSubviewToFront:badgeView];
        [self setBadgeAnimateView:badgeView];
        [self addSubview:badgeView];
        [self addLayoutConstraint];
    }
    return badgeView;
}

- (void)setBadgeAnimateView:(ZFBadgeAnimationView *)badgeAnimateView{
    objc_setAssociatedObject(self, &keyBadgeTipView, badgeAnimateView, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
- (NSMutableArray *)items{
    NSMutableArray *arr = objc_getAssociatedObject(self, &keyItems);
    if (!arr) {
        arr = [NSMutableArray array];
        [self setItems:arr];
    }
    return arr;
}
- (void)setItems:(NSMutableArray *)items{
    objc_setAssociatedObject(self, &keyItems, items, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

@end
